/**
************************************************************
* @file         gizwits_product.c
* @brief        Gizwits control protocol processing, and platform-related       hardware initialization 
* @author       Gizwits
* @date         2017-07-19
* @version      V03030000
* @copyright    Gizwits
* 
* @note         机智云.只为智能硬件而生
*               Gizwits Smart Cloud  for Smart Products
*               链接|增值ֵ|开放|中立|安全|自有|自由|生态
*               www.gizwits.com
*
***********************************************************/

#include <stdio.h>
#include <string.h>
#include "Hal_Usart/hal_uart.h"
#include "gizwits_protocol.h"
#include "gizwits_product.h"
#include "common.h"

extern uint32_t timerMsCount;

/** User area the current device state structure*/
extern dataPoint_t currentDataPoint;

/**@} */
/**@name Gizwits User Interface
* @{
*/

/**
* @brief Event handling interface

* Description:

* 1. Users can customize the changes in WiFi module status

* 2. Users can add data points in the function of event processing logic, such as calling the relevant hardware peripherals operating interface

* @param [in] info: event queue
* @param [in] data: protocol data
* @param [in] len: protocol data length
* @return NULL
* @ref gizwits_protocol.h
*/
int8_t gizwitsEventProcess(eventInfo_t *info, uint8_t *gizdata, uint32_t len)
{
  uint8_t i = 0;
  dataPoint_t *dataPointPtr = (dataPoint_t *)gizdata;
  moduleStatusInfo_t *wifiData = (moduleStatusInfo_t *)gizdata;
  protocolTime_t *ptime = (protocolTime_t *)gizdata;
  
#if MODULE_TYPE
  gprsInfo_t *gprsInfoData = (gprsInfo_t *)gizdata;
#else
  moduleInfo_t *ptModuleInfo = (moduleInfo_t *)gizdata;
#endif

  if((NULL == info) || (NULL == gizdata))
  {
    return -1;
  }

  for(i=0; i<info->num; i++)
  {
    switch(info->event[i])
    {
      case EVENT_MO1:
        currentDataPoint.valueMO1 = dataPointPtr->valueMO1;
        GIZWITS_LOG("Evt: EVENT_MO1 %d \n", currentDataPoint.valueMO1);
        if(0x01 == currentDataPoint.valueMO1)
        {
          //user handle
        }
        else
        {
          //user handle    
        }
        break;
      case EVENT_MO2:
        currentDataPoint.valueMO2 = dataPointPtr->valueMO2;
        GIZWITS_LOG("Evt: EVENT_MO2 %d \n", currentDataPoint.valueMO2);
        if(0x01 == currentDataPoint.valueMO2)
        {
          //user handle
        }
        else
        {
          //user handle    
        }
        break;
      case EVENT_ON_OFF:
        currentDataPoint.valueON_OFF = dataPointPtr->valueON_OFF;
        GIZWITS_LOG("Evt: EVENT_ON_OFF %d \n", currentDataPoint.valueON_OFF);
        if(0x01 == currentDataPoint.valueON_OFF)
        {
          //user handle
        }
        else
        {
          //user handle    
        }
        break;

      case EVENT_MODE:
        currentDataPoint.valueMODE = dataPointPtr->valueMODE;
        GIZWITS_LOG("Evt: EVENT_MODE %d\n", currentDataPoint.valueMODE);
        switch(currentDataPoint.valueMODE)
        {
          case MODE_VALUE0:
            //user handle
            break;
          case MODE_VALUE1:
            //user handle
            break;
          case MODE_VALUE2:
            //user handle
            break;
          case MODE_VALUE3:
            //user handle
            break;
          case MODE_VALUE4:
            //user handle
            break;
          case MODE_VALUE5:
            //user handle
            break;
          case MODE_VALUE6:
            //user handle
            break;
          case MODE_VALUE7:
            //user handle
            break;
          case MODE_VALUE8:
            //user handle
            break;
          default:
            break;
        }
        break;
      case EVENT_MOspeed:
        currentDataPoint.valueMOspeed = dataPointPtr->valueMOspeed;
        GIZWITS_LOG("Evt: EVENT_MOspeed %d\n", currentDataPoint.valueMOspeed);
        switch(currentDataPoint.valueMOspeed)
        {
          case MOspeed_VALUE0:
            //user handle
            break;
          case MOspeed_VALUE1:
            //user handle
            break;
          case MOspeed_VALUE2:
            //user handle
            break;
          case MOspeed_VALUE3:
            //user handle
            break;
          case MOspeed_VALUE4:
            //user handle
            break;
          case MOspeed_VALUE5:
            //user handle
            break;
          case MOspeed_VALUE6:
            //user handle
            break;
          case MOspeed_VALUE7:
            //user handle
            break;
          case MOspeed_VALUE8:
            //user handle
            break;
          default:
            break;
        }
        break;
      case EVENT_Y_ON:
        currentDataPoint.valueY_ON = dataPointPtr->valueY_ON;
        GIZWITS_LOG("Evt: EVENT_Y_ON %d\n", currentDataPoint.valueY_ON);
        switch(currentDataPoint.valueY_ON)
        {
          case Y_ON_VALUE0:
            //user handle
            break;
          case Y_ON_VALUE1:
            //user handle
            break;
          case Y_ON_VALUE2:
            //user handle
            break;
          case Y_ON_VALUE3:
            //user handle
            break;
          case Y_ON_VALUE4:
            //user handle
            break;
          case Y_ON_VALUE5:
            //user handle
            break;
          case Y_ON_VALUE6:
            //user handle
            break;
          case Y_ON_VALUE7:
            //user handle
            break;
          case Y_ON_VALUE8:
            //user handle
            break;
          case Y_ON_VALUE9:
            //user handle
            break;
          default:
            break;
        }
        break;
      case EVENT_Y_OFF:
        currentDataPoint.valueY_OFF = dataPointPtr->valueY_OFF;
        GIZWITS_LOG("Evt: EVENT_Y_OFF %d\n", currentDataPoint.valueY_OFF);
        switch(currentDataPoint.valueY_OFF)
        {
          case Y_OFF_VALUE0:
            //user handle
            break;
          case Y_OFF_VALUE1:
            //user handle
            break;
          case Y_OFF_VALUE2:
            //user handle
            break;
          case Y_OFF_VALUE3:
            //user handle
            break;
          case Y_OFF_VALUE4:
            //user handle
            break;
          case Y_OFF_VALUE5:
            //user handle
            break;
          case Y_OFF_VALUE6:
            //user handle
            break;
          case Y_OFF_VALUE7:
            //user handle
            break;
          case Y_OFF_VALUE8:
            //user handle
            break;
          case Y_OFF_VALUE9:
            //user handle
            break;
          default:
            break;
        }
        break;
      case EVENT_MO1MO2:
        currentDataPoint.valueMO1MO2 = dataPointPtr->valueMO1MO2;
        GIZWITS_LOG("Evt: EVENT_MO1MO2 %d\n", currentDataPoint.valueMO1MO2);
        switch(currentDataPoint.valueMO1MO2)
        {
          case MO1MO2_VALUE0:
            //user handle
            break;
          case MO1MO2_VALUE1:
            //user handle
            break;
          case MO1MO2_VALUE2:
            //user handle
            break;
          default:
            break;
        }
        break;



      case WIFI_SOFTAP:
        break;
      case WIFI_AIRLINK:
        break;
      case WIFI_STATION:
        break;
      case WIFI_CON_ROUTER:
 
        break;
      case WIFI_DISCON_ROUTER:
 
        break;
      case WIFI_CON_M2M:
 
        break;
      case WIFI_DISCON_M2M:
        break;
      case WIFI_RSSI:
        GIZWITS_LOG("RSSI %d\n", wifiData->rssi);
        break;
      case TRANSPARENT_DATA:
        GIZWITS_LOG("TRANSPARENT_DATA \n");
        //user handle , Fetch data from [data] , size is [len]
        break;
      case WIFI_NTP:
        GIZWITS_LOG("WIFI_NTP : [%d-%d-%d %02d:%02d:%02d][%d] \n",ptime->year,ptime->month,ptime->day,ptime->hour,ptime->minute,ptime->second,ptime->ntp);
        break;
      case MODULE_INFO:
            GIZWITS_LOG("MODULE INFO ...\n");
      #if MODULE_TYPE
            GIZWITS_LOG("GPRS MODULE ...\n");
            //Format By gprsInfo_t
      #else
            GIZWITS_LOG("WIF MODULE ...\n");
            //Format By moduleInfo_t
            GIZWITS_LOG("moduleType : [%d] \n",ptModuleInfo->moduleType);
      #endif
    break;
      default:
        break;
    }
  }

  return 0;
}

/**
* User data acquisition

* Here users need to achieve in addition to data points other than the collection of data collection, can be self-defined acquisition frequency and design data filtering algorithm

* @param none
* @return none
*/
void userHandle(void)
{
 /*

    */
    
}

/**
* Data point initialization function

* In the function to complete the initial user-related data
* @param none
* @return none
* @note The developer can add a data point state initialization value within this function
*/
void userInit(void)
{
    memset((uint8_t*)&currentDataPoint, 0, sizeof(dataPoint_t));
    
    /** Warning !!! DataPoint Variables Init , Must Within The Data Range **/ 
    /*
      currentDataPoint.valueMO1 = ;
      currentDataPoint.valueMO2 = ;
      currentDataPoint.valueON_OFF = ;
      currentDataPoint.valueMODE = ;
      currentDataPoint.valueMOspeed = ;
      currentDataPoint.valueY_ON = ;
      currentDataPoint.valueY_OFF = ;
      currentDataPoint.valueMO1MO2 = ;
    */

}


/**
* @brief Millisecond timing maintenance function, milliseconds increment, overflow to zero

* @param none
* @return none
*/
void gizTimerMs(void)
{
    timerMsCount++;
}

/**
* @brief Read millisecond count

* @param none
* @return millisecond count
*/
uint32_t gizGetTimerCount(void)
{
    return timerMsCount;
}

/**
* @brief MCU reset function

* @param none
* @return none
*/
void mcuRestart(void)
{
        IAP_CONTR &= ~0x40; //软件复位后直接执行用户代码
        IAP_CONTR |= 0x20;  //软件复位
}

/**@} */

/**

/**
* @brief Serial port write operation, send data to WiFi module
*
* @param buf      : buf address
* @param len      : buf length
*
* @return : Return effective data length;-1，return failure
*/
int32_t uartWrite(uint8_t *buf, uint32_t len)
{
    uint32_t i = 0;
    
    if(NULL == buf)
    {
        return -1;
    }

#ifdef PROTOCOL_DEBUG
    GIZWITS_LOG("MCU2WiFi[%lu:%lu]: ", gizGetTimerCount(), len);
    for(i=0; i<len; i++)
    {
        GIZWITS_LOG("%02x ", (uint16_t)buf[i]);

        if(i >=2 && buf[i] == 0xFF)
        {
            GIZWITS_LOG("%02x ", 0x55);
        }
    }

    GIZWITS_LOG("\n");
#endif
    
    for(i=0; i<len; i++)
    {
        UART1_TxByte(buf[i]);

        if(i >=2 && buf[i] == 0xFF)
        {
          UART1_TxByte(0x55);
        }
    }
        
    return len;
}


